package org.neuedu.servlet;

import org.neuedu.bean.User;
import org.neuedu.service.UserService;
import org.neuedu.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserRegServlet",urlPatterns = "/user/reg")
public class UserRegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 请求参数 邮箱，密码
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
        String username = request.getParameter("username");
        User user = new User();
        user.setEmail(email);
        user.setPassword(pass);
        user.setNickName(username);
        // 调service
        UserService service = new UserServiceImpl();
        int i = service.userReg(user);
        if(i == 0){
            request.setAttribute("msg","邮箱已经存在");
            request.getRequestDispatcher("/WEB-INF/jsp/user/reg.jsp").forward(request,response);
        }else{
            response.sendRedirect(request.getContextPath()+"/forward/log");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
